﻿using System;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data.OleDb;
using System.Data.SqlClient;
using Negocio;

namespace Persistencia
{
    class BrokerInjury : Broker
    {
        public BrokerInjury()
        {
            this.conn = ConnectionDB.getInstance();
        }

        public override void add(Object o)
        {
            Injury InjuryAux = (Injury)o;
            this.conn.connect();
            string sql = "insert into Injury(Oid, name, status, date_, type, severity, description, affectedArea, treatmentType, cause, activityWhenInjured, comments) values('" + InjuryAux.OID + "', '" + InjuryAux.Name + "', '" + InjuryAux.Status + "','" + InjuryAux.Date + "','" + InjuryAux.Type + "','" + InjuryAux.Severity + "','" + InjuryAux.Description + "','" + InjuryAux.AffectedArea + "','" + InjuryAux.TreatmentType + "', '" + InjuryAux.Cause + "','" + InjuryAux.ActivityWhenInjured + "', '" + InjuryAux.Comments + "')";
            string sql1 = "update OID set OId=" + InjuryAux.OID;
            this.conn.execute(sql);
            this.conn.execute(sql1);
            //this.conn.disconnect();
        }

        public override void update(Object o)
        {
            Injury InjuryAux = (Injury)o;
            string sql = "update Injury set status='"
                + InjuryAux.Status + "', date_ ='" + InjuryAux.Date
                + "',type = '" + InjuryAux.Type + "',severity = '"
                + InjuryAux.Severity + "',description = '" + InjuryAux.Description + "',affectedArea= '"
                + InjuryAux.AffectedArea + "',treatmentType = '" + InjuryAux.TreatmentType + "',activityWhenInjured = '"
                + InjuryAux.ActivityWhenInjured + "', name = '" + InjuryAux.Name + "', cause = '" + InjuryAux.Cause + "', comments = '" + InjuryAux.Comments + "' where oid = " + InjuryAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            //this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            Injury InjuryAux = (Injury)o;
            string sql = "delete  Injury  where oid=" + InjuryAux.OID;
            //this.conn.connect();
            this.conn.execute(sql);
            //this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            Injury InjuryAux = (Injury)o;
            this.conn.connect();
            string sql = "Select * from Injury where oid =" + InjuryAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                InjuryAux.Status = this.conn.getDataString("status").ToString();
                InjuryAux.Date = (DateTime)this.conn.getDataString("Date_");
                InjuryAux.Type = this.conn.getDataString("type").ToString();
                InjuryAux.Severity = this.conn.getDataString("severity").ToString();
                InjuryAux.Description = this.conn.getDataString("description").ToString();
                InjuryAux.AffectedArea = this.conn.getDataString("affectedArea").ToString();
                InjuryAux.ActivityWhenInjured = this.conn.getDataString("activityWhenInjured").ToString();
                InjuryAux.Name = this.conn.getDataString("Name").ToString();
                InjuryAux.Comments = this.conn.getDataString("Comments").ToString();
                InjuryAux.Cause = this.conn.getDataString("cause").ToString();
                InjuryAux.TreatmentType = this.conn.getDataString("treatmentType").ToString();
                InjuryAux.OID = int.Parse(this.conn.getDataString("oid").ToString());
            }
            this.conn.disconnect();

        }
        public override ObservableCollection<Object> bringList(Object o)
        {
            string sql = "Select * from Injury order by description asc";
            ObservableCollection<Object> lista = new ObservableCollection<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                Injury InjuryAux = (Injury)o;
                InjuryAux.Status = this.conn.getDataString("status").ToString();
                InjuryAux.Date = (DateTime)this.conn.getDataString("Date_");
                InjuryAux.Type = this.conn.getDataString("type").ToString();
                InjuryAux.Severity = this.conn.getDataString("severity").ToString();
                InjuryAux.Description = this.conn.getDataString("description").ToString();
                InjuryAux.AffectedArea = this.conn.getDataString("affectedArea").ToString();
                InjuryAux.ActivityWhenInjured = this.conn.getDataString("activityWhenInjured").ToString();
                InjuryAux.Name = this.conn.getDataString("Name").ToString();
                InjuryAux.Comments = this.conn.getDataString("Comments").ToString();
                InjuryAux.Cause = this.conn.getDataString("cause").ToString();
                InjuryAux.TreatmentType = this.conn.getDataString("treatmentType").ToString();
                InjuryAux.OID = int.Parse(this.conn.getDataString("oid").ToString());
                lista.Add(InjuryAux);
            }
            this.conn.disconnect();
            return lista;
        }

    }
}
